package com.metadata_system.ai.service;

import com.metadata_system.ai.request.MetadataEdgeListRequest;
import com.metadata_system.ai.request.MetadataListRequest;
import com.metadata_system.ai.request.MetadataNodeListRequest;
import com.metadata_system.ai.request.MetadataViewListRequest;
import com.metadata_system.ai.response.MetadataEdgeResponse;
import com.metadata_system.ai.response.MetadataNodeResponse;
import com.metadata_system.ai.response.MetadataResponse;
import com.metadata_system.ai.response.MetadataViewResponse;
import com.metadata_system.common.db.entity.Metadata;
import com.metadata_system.common.db.entity.MetadataEdge;
import com.metadata_system.common.db.entity.MetadataNode;
import com.metadata_system.common.db.entity.MetadataView;
import com.metadata_system.common.db.mapper.MetadataEdgeMapper;
import com.metadata_system.common.db.mapper.MetadataMapper;
import com.metadata_system.common.db.mapper.MetadataNodeMapper;
import com.metadata_system.common.db.mapper.MetadataViewMapper;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.service.UserOperationLogService;
import com.metadata_system.common.utils.DateTimeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("aiServerMetadataService")
public class MetadataService {
    @Autowired(required = false)
    MetadataMapper metadataMapper;
    @Autowired(required = false)
    MetadataViewMapper metadataViewMapper;
    @Autowired(required = false)
    MetadataNodeMapper metadataNodeMapper;
    @Autowired(required = false)
    MetadataEdgeMapper metadataEdgeMapper;
    @Autowired
    UserOperationLogService userOperationLogService;


    public ApiResult getList(MetadataListRequest listRequest) {
        Example example = new Example(Metadata.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andGreaterThan("dataTotal", 0);
        if(!StringUtils.isBlank(listRequest.getUpdatedTime())){
            try {
                Date date = DateTimeUtil.parseToDate(listRequest.getUpdatedTime(), DateTimeUtil.FMT_yyyyMMddHHmmss);
                criteria.andGreaterThan("updatedTime", date);
            } catch (Exception e) {
                return ApiResult.error("时间格式错误");
            }
        }
        listRequest.setOrder(example, "updatedTime");
        int total = this.metadataMapper.selectCountByExample(example);
        List<Metadata> list=new ArrayList<>();
        if (total > 0) {
            list = this.metadataMapper.selectByExampleAndRowBounds(example,listRequest.getRowRunds());
        }

        return ApiResult.syncList(MetadataResponse.fromEntity(list), total,new ArrayList<>());
    }

    public ApiResult getViewList(MetadataViewListRequest listRequest) {
        Example example = new Example(MetadataView.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isBlank(listRequest.getMetadataIds())){
            List<Integer> metadataIds = Arrays.stream(listRequest.getMetadataIds().split(",")).map(Integer::valueOf).collect(Collectors.toList());
            criteria.andIn("metadataId", metadataIds);
        }
        if(!StringUtils.isBlank(listRequest.getUpdatedTime())){
            try {
                Date date = DateTimeUtil.parseToDate(listRequest.getUpdatedTime(), DateTimeUtil.FMT_yyyyMMddHHmmss);
                criteria.andGreaterThan("updatedTime", date);
            } catch (Exception e) {
                return ApiResult.error("时间格式错误");
            }
        }
        listRequest.setOrder(example, "updatedTime");
        int total = this.metadataViewMapper.selectCountByExample(example);
        List<MetadataView> list=new ArrayList<>();
        if (total > 0) {
            list = this.metadataViewMapper.selectByExampleAndRowBounds(example,listRequest.getRowRunds());
        }
        return ApiResult.syncList(MetadataViewResponse.fromEntity(list), total,new ArrayList<>());
    }

    public ApiResult getNodeList(MetadataNodeListRequest listRequest) {
        Example example = new Example(MetadataNode.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isBlank(listRequest.getUpdatedTime())){
            try {
                Date date = DateTimeUtil.parseToDate(listRequest.getUpdatedTime(), DateTimeUtil.FMT_yyyyMMddHHmmss);
                criteria.andGreaterThan("updatedTime", date);
            } catch (Exception e) {
                return ApiResult.error("时间格式错误");
            }
        }
        if(!StringUtils.isBlank(listRequest.getMetadataViewIds())){
            List<Integer> metadataViewIds = Arrays.stream(listRequest.getMetadataViewIds().split(",")).map(Integer::valueOf).collect(Collectors.toList());
            criteria.andIn("metadataViewId", metadataViewIds);
        }
        if(!StringUtils.isBlank(listRequest.getMetadataIds())){
            List<Integer> metadataIds = Arrays.stream(listRequest.getMetadataIds().split(",")).map(Integer::valueOf).collect(Collectors.toList());
            criteria.andIn("metadataId", metadataIds);
        }
        listRequest.setOrder(example, "updatedTime");
        int total = this.metadataNodeMapper.selectCountByExample(example);
        List<MetadataNode> list=new ArrayList<>();
        if (total > 0) {
            list = this.metadataNodeMapper.selectByExampleAndRowBounds(example,listRequest.getRowRunds());
        }
        return ApiResult.syncList(MetadataNodeResponse.fromEntity(list), total,new ArrayList<>());
    }


    public ApiResult getEdgeList(MetadataEdgeListRequest listRequest) {
        Example example = new Example(MetadataEdge.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isBlank(listRequest.getUpdatedTime())){
            try {
                Date date = DateTimeUtil.parseToDate(listRequest.getUpdatedTime(), DateTimeUtil.FMT_yyyyMMddHHmmss);
                criteria.andGreaterThan("updatedTime", date);
            } catch (Exception e) {
                return ApiResult.error("时间格式错误");
            }
        }
        if(!StringUtils.isBlank(listRequest.getMetadataViewIds())){
            List<Integer> metadataViewIds = Arrays.stream(listRequest.getMetadataViewIds().split(",")).map(Integer::valueOf).collect(Collectors.toList());
            criteria.andIn("metadataViewId", metadataViewIds);
        }
        if(!StringUtils.isBlank(listRequest.getMetadataIds())){
            List<Integer> metadataIds = Arrays.stream(listRequest.getMetadataIds().split(",")).map(Integer::valueOf).collect(Collectors.toList());
            criteria.andIn("metadataId", metadataIds);
        }
        listRequest.setOrder(example, "updatedTime");
        int total = this.metadataEdgeMapper.selectCountByExample(example);
        List<MetadataEdge> list=new ArrayList<>();
        if (total > 0) {
            list = this.metadataEdgeMapper.selectByExampleAndRowBounds(example,listRequest.getRowRunds());
        }
        return ApiResult.syncList(MetadataEdgeResponse.fromEntity(list), total,new ArrayList<>());
    }
}
